package ysq.xyg.host.controller;

import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.host.model.po.HostArea;
import ysq.xyg.host.model.po.HostReport;
import ysq.xyg.host.model.vo.HostAreaVo;
import ysq.xyg.host.model.vo.HostReportVo;
import ysq.xyg.host.service.HostAreaService;
import ysq.xyg.host.service.HostReportService;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 房产模块-报备controller层
 *
 * @author shengQiang yu
 * @date 2019-7-1 15:25:15
 */
@RestController
public class HostReportController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(HostReportController.class);
    @Resource
    private HostReportService hostReportService;
    @Resource
    private HostAreaService hostAreaService;

    private HostReport hostReport;

    /**
     * 分页查询
     *
     * @param hostReportVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-7-1 15:25:15
     */
    @BaseLog(module = "房产模块",className = "报备",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostReport/page",method = RequestMethod.POST)
    public ReturnEntity page(HttpServletRequest request,HostReportVo hostReportVo, int pageSize, int pageNum){
        SysUser sysUser=acceptCurrentUser(request);
        //用户类型：1、总权限管理员，2、区域负责管理员
        if("2".equals(sysUser.getUserType())){
            List<HostArea> hostAreaList=hostAreaService.acceptList(new HostAreaVo("",sysUser.getId()));
            if(hostAreaList!=null&&hostAreaList.size()>0){
                String[] areas=new String[hostAreaList.size()];
                for(int i=0;i<hostAreaList.size();i++){
                    areas[i]=hostAreaList.get(i).getId();
                }
                hostReportVo.setAreaIds(areas);
            }else{
                //当用户没有时，给用户设置区域为不存在的数据
                hostReportVo.setAreaIds(new String[]{"xyg12212121"});
            }
        }
        ReturnEntity returnEntity = hostReportService.pageMember(hostReportVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-7-1 15:25:15
     */
    @RequestMapping(value = "/hostReport/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(HttpServletRequest request,String id){
        SysUser sysUser=acceptCurrentUser(request);
        ModelAndView view = new ModelAndView("/host/hostReportUpdateOrSave");
        //用户类型：1、总权限管理员，2、区域负责管理员
        if("2".equals(sysUser.getUserType())){
            List<HostArea> hostAreaList=hostAreaService.acceptList(new HostAreaVo("",sysUser.getId()));
            if(hostAreaList!=null&&hostAreaList.size()>0){
                String[] areas=new String[hostAreaList.size()];
                for(int i=0;i<hostAreaList.size();i++){
                    areas[i]=hostAreaList.get(i).getId();
                }
                view.addObject("areaIds", areas.toString());
            }
        }

        if(StrUtil.isNotEmpty(id)){
            hostReport = hostReportService.selectByPrimaryKey(id);
            view.addObject("hostReport", hostReport);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-7-1 15:25:15
     */
    @BaseLog(module = "房产模块",className = "报备",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostReport/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return hostReportService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param hostReportVo Vo对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:15
     */
    @BaseLog(module = "房产模块",className = "报备",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostReport/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,HostReportVo hostReportVo){
        return hostReportService.addOrUpdate(hostReportVo,acceptCurrentUser(request));
    }
    /**
     * 修改
     *
     * @param hostReportVo Vo对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:15
     */
    @BaseLog(module = "房产模块",className = "报备",methods = "修改状态",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/hostReport/update",method = RequestMethod.POST)
    public ReturnEntity update(HttpServletRequest request,HostReportVo hostReportVo){
        return hostReportService.update(hostReportVo,acceptCurrentUser(request));
    }
}